//
//  ViewController.m
//  CardSlide
//
//  Created by DavidWang on 15/11/25.
//  Copyright © 2015年 DavidWang. All rights reserved.
//

#import "ViewController.h"
#import "DWViewCell.h"
#import "DWFlowLayout.h"
#import "Size.h"
#import "MicroMedicalViewController.h"
#import "TheMessageViewController.h"
#import "FoundViewController.h"
#import "MineViewController.h"
#import "AppDelegate.h"
#define SCREENWITH   [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *data;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    DWFlowLayout *layout = [[DWFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWITH, SCREENHEIGHT) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.collectionViewLayout = layout;
    data = @[@"01.png",@"02.png",@"03.png"];
    [_collectionView reloadData];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[DWViewCell class] forCellWithReuseIdentifier:@"DWViewCell"];
    self.view.backgroundColor = GRAYCOLOR;
    _collectionView.backgroundColor = GRAYCOLOR;
    
    
    
    
    //显示整张照片 默认NO
    _collectionView.pagingEnabled = YES;
    //边境反弹效果 默认YES
    _collectionView.bounces = YES;
    //显示或隐藏 下面指示条  默认开启
    //_collectionView.showsHorizontalScrollIndicator = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    if (_collectionView.contentOffset.x == 325) {
        CGPoint position = CGPointMake(325, 0);
        [_collectionView setContentOffset:position animated:YES];
    }
}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//    //NSLog(@"scrollViewDidEndDecelerating");
//        if (_collectionView.contentOffset.x == 0) {
//            CGPoint position = CGPointMake(325, 0);
//            [_collectionView setContentOffset:position animated:YES];
//        }
//        if (_collectionView.contentOffset.x == 325) {
//            CGPoint position = CGPointMake(645, 0);
//            [_collectionView setContentOffset:position animated:YES];
//        }
//    //[_collectionView setContentOffset:CGPointMake(325, 0) animated:YES];
//    
//}
//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    NSLog(@"123");
//    if (_collectionView.contentOffset.x == 0) {
//        CGPoint position = CGPointMake(325, 0);
//        [_collectionView setContentOffset:position animated:YES];
//    }
//    if (_collectionView.contentOffset.x == 325) {
//        CGPoint position = CGPointMake(645, 0);
//        [_collectionView setContentOffset:position animated:YES];
//    }
//}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    
//    NSLog(@"scrollViewWillBeginDragging");
//        if (scrollView.contentOffset.x == 0) {
//            CGPoint position = CGPointMake(325, 0);
//            [scrollView setContentOffset:position animated:YES];
//        }
//        if (scrollView.contentOffset.x == 325) {
//            CGPoint position = CGPointMake(645, 0);
//            [scrollView setContentOffset:position animated:YES];
//        }
//    
//}

#pragma mark cell的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return data.count;
}

#pragma mark cell的视图
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DWViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DWViewCell" forIndexPath:indexPath];
    
    
    NSInteger row = indexPath.row;
    cell.showImg.image = [UIImage imageNamed:[data objectAtIndex:row]];
    
    return cell;
}

#pragma mark cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREENWITH - 70, SCREENHEIGHT - 80 - 80);
}

#pragma mark cell的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        NSLog(@"333");

        AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
        appdelegate.window.rootViewController = appdelegate.naVC;
    }
}

@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com